package com.test;

import com.test.mapper.UserMapper;
import com.test.entity.User;
import com.test.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class UserMybatis {

    private SqlSession sqlSession;

    private UserMapper userMapper;

    @Before
    public void setUp() {
        // 创建测试表
//        DatabaseUtil.createTestTable();

        // 获取SqlSession和Mapper
        sqlSession = MyBatisUtil.getSqlSession();
        userMapper = sqlSession.getMapper(UserMapper.class);

        System.out.println("🚀 开始执行MyBatis CRUD测试...");
    }

    @Test
    public void insertUser(){
        User user = new User("张三123", "111@qq.com", 18);
        int num = userMapper.insertUser(user);
        System.out.println("插入成功，条数是：" + num);
    }

    @Test
    public void queryList(){
        List<User> userList = userMapper.queryUserList();
        userList.forEach(bean ->{
            System.out.println(bean.getName());
        });
    }

    @Test
    public void updateUserById(){
        User user = new User(1, "张三12", "66@qq.com", 18);
        userMapper.updateUserById(user);
        System.out.println("修改成功。");
    }

    @Test
    public void deleteUserById(){
        userMapper.deleteUserById(null);
        System.out.println("删除成功。");
    }

}
